package com.fjwt.gz.mch.ctrl.score.exchange;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.ScoreExchangeDTO;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.vo.ScoreExchangeFansVO;
import com.fjwt.gz.db.vo.ScoreExchangeVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AgentInfoService;
import com.fjwt.gz.service.impl.ScoreExchangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 积分商城接口类
 */
@Slf4j
@RestController
@RequestMapping("/api/scoreExchange")
public class ScoreExchangeController extends CommonCtrl {
    @Autowired
    private ScoreExchangeService exchangeService;
    @Autowired
    private AgentInfoService agentInfoService;

    /**
     * 获取积分商城列表
     */
    @PostMapping("/getList")
    public ApiRes getScoreExchange(){
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        scoreExchangeDTO.setType(0);
        scoreExchangeDTO.setAppId(getCurrentUser().getAppId());
        List<AgentInfoEntity> agentInfoEntityList = new ArrayList<>();
        agentInfoService.queryAllTopAgentNo(agentInfoEntityList, scoreExchangeDTO.getAgentNo());
        scoreExchangeDTO.setAgentNoList(agentInfoEntityList.stream().map(AgentInfoEntity::getAgentNo).toList());
        scoreExchangeDTO.setAgentNo(null);
        scoreExchangeDTO.setSysType(CS.SYS_ROLE_TYPE.MCH);
        scoreExchangeDTO.setFansId(getCurrentUser().getFansId());
        log.info("fans端积分商城列表请求参数：scoreExchangeDTO:{}", scoreExchangeDTO);
        IPage<ScoreExchangeVO> list = exchangeService.queryList(getIPage(), scoreExchangeDTO);
        log.info("fans端积分商城列表返回参数，请求地址：/api/scoreExchange/getList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 获取权益商城列表
     */
    @PostMapping("/equityMallList")
    public ApiRes getEquityMallList(){
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        scoreExchangeDTO.setType(1);
        scoreExchangeDTO.setAppId(getCurrentUser().getAppId());
        List<AgentInfoEntity> agentInfoEntityList = new ArrayList<>();
        agentInfoService.queryAllTopAgentNo(agentInfoEntityList, scoreExchangeDTO.getAgentNo());
        scoreExchangeDTO.setAgentNoList(agentInfoEntityList.stream().map(AgentInfoEntity::getAgentNo).toList());
        scoreExchangeDTO.setAgentNo(null);
        scoreExchangeDTO.setSysType(CS.SYS_ROLE_TYPE.MCH);
        scoreExchangeDTO.setFansId(getCurrentUser().getFansId());
        log.info("fans端积分商城列表请求参数，请求地址：/api/scoreExchange/equityMallList，请求参数：scoreExchangeDTO：{}",  scoreExchangeDTO);
        IPage<ScoreExchangeVO> list = exchangeService.queryList(getIPage(),scoreExchangeDTO);
        log.info("fans端积分商城列表返回参数，请求地址：/api/scoreExchange/equityMallList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 获取积分兑换明细
     */
    @PostMapping("/getConsumeList")
    public ApiRes getConsumeList(){
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        log.info("fans端获取积分兑换明细，请求地址：/api/scoreExchange/getConsumeList，请求参数：{}", JSONUtil.toJsonStr(scoreExchangeDTO));
        IPage<ScoreExchangeFansVO> list = exchangeService.selectConsumeList(getIPage(), scoreExchangeDTO);
        log.info("fans端获取积分兑换明细返回参数，请求地址：/api/scoreExchange/getConsumeList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 积分商城详情
     */
    @PostMapping("/getDetails")
    public ApiRes getDetails(){
        Long exchangeId = getValLong("scoreExchangeId");
        String awardType = getValString("awardType");
        log.info("fans端积分商城详情请求参数，请求地址：/api/scoreExchange/getDetails，请求参数：exchangeId：{}，awardType：{}",  exchangeId, awardType);
        ScoreExchangeVO vo = exchangeService.getDetails(exchangeId, awardType);
        log.info("fans端积分商城详情返回参数，请求地址：/api/scoreExchange/getDetails，返回结果：{}", JSONUtil.toJsonStr(vo));
        ScoreExchangeVO dto = new ScoreExchangeVO();
        BeanUtils.copyProperties(vo,dto);
        return ApiRes.ok(dto);
    }

}
